package com.config;


import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.*;
import org.springframework.core.io.ClassPathResource;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping;
import org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter;

import java.nio.charset.Charset;
import java.util.Arrays;
import java.util.List;

@Configuration
@EnableWebMvc
@ComponentScan("com.*")
@EnableTransactionManagement
@EnableCaching
@EnableScheduling
@EnableAspectJAutoProxy
@Import({JdbcConfig.class,MyBatisConfig.class,WebConfiguration.class,FileUploadConfig.class,TransactionConfig.class,RedisConfig.class,CacheConfig.class,MongodbConfig.class})
public class SpringConfiguration extends WebMvcConfigurerAdapter {
    @Bean("propertyConfigurer")  //propertie的配置器将指定的properies添加
    public PropertyPlaceholderConfigurer getPropertyPalce(){
        PropertyPlaceholderConfigurer propertyPlaceholderConfigurer = 	new PropertyPlaceholderConfigurer();//生成配置类
        //生成资源数组
        ClassPathResource[]  classPathResources = {new ClassPathResource("c3p0.properties"),
                new ClassPathResource("log4j.properties"),
                new ClassPathResource("log4jdbc.log4j2.properties"),
                new ClassPathResource("redis.properties"),
                new ClassPathResource("mongodb.properties")
        };
        //classpth就是部署出来的classes的根目录
        propertyPlaceholderConfigurer.setLocations(classPathResources);//将指定资源注册加载
        return propertyPlaceholderConfigurer;
    }

    //无外乎就是  覆盖 和  定义返回值（@Bean）


//    // 静态资源访问
    @Override
    public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
        configurer.enable();
    }
//添加绑定的资源，参数registry从来哪里来,spring给的 spring将所以资源装入自己，也生成资源注册器，让这个资源注册器把静态资源注册了，以前这个资源注册器只注册了和controller绑定的url
//    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        //addResourceHandler("/static/**") 表示将所有的static下的资源请映射到后面指定的路径.addResourceLocations("classpath:/static/")
        //如果要访问css下资源 请到classpath 服务器目录下的static下的css文件夹下面去找
        //就能找到因为 资源注册器把这里面的资源和url都绑好了 按文件名绑好了
        //webapp中
        registry.addResourceHandler("/css/**").addResourceLocations("classpath:/static/css/").setCachePeriod(31556926);
        registry.addResourceHandler("/html/**").addResourceLocations("classpath:/static/html/").setCachePeriod(31556926);
    }
    //配置 ResponseBoby 输出list对象的转换器，将jackson提供的转换器 设置给springmvc
//    @Override
    public void extendMessageConverters(List<HttpMessageConverter<?>> messageConverters) {
        MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
        MediaType[] supportedMediaTypes = {new MediaType("text", "html", Charset.forName("utf-8"))};

        converter.setSupportedMediaTypes(Arrays.asList(supportedMediaTypes));
//        for (MediaType mediaType : converter.getSupportedMediaTypes()) {
//            System.out.println(mediaType.getType() + "-----" + mediaType.getSubtype());
//        }
        messageConverters.set(6, converter);//为什么这个参数是6，因为默认7个类型转换器，他是第7个
        int count = 0;
        for (HttpMessageConverter httpMessageConverter : messageConverters) {
            System.out.println(httpMessageConverter.getClass().getSimpleName() + "  --  " +count);
            List<MediaType> mediaTypes = httpMessageConverter.getSupportedMediaTypes();
            for (MediaType mediaType : mediaTypes) {
                System.out.println(mediaType.getType() + " " + mediaType.getSubtype());
            }
            count++;
        }
    }

}

//        registry.addResourceHandler("/js1/**").addResourceLocations("/js1/").setCachePeriod(31556926);
//        registry.addResourceHandler("/js/**").addResourceLocations("classpath:/static/js/").setCachePeriod(31556926);
//
//        registry.addResourceHandler("/back/css/**").addResourceLocations("classpath:/static/back/css/").setCachePeriod(31556926);
//        registry.addResourceHandler("/back/js/**").addResourceLocations("classpath:/static/back/js/").setCachePeriod(31556926);
//        registry.addResourceHandler("/front/chart/css/**").addResourceLocations("classpath:/static/front/chart/css/").setCachePeriod(31556926);
//        registry.addResourceHandler("/front/chart/js/**").addResourceLocations("classpath:/static/front/chart/js/").setCachePeriod(31556926);
//        registry.addResourceHandler("/front/css/**").addResourceLocations("classpath:/static/front/css/").setCachePeriod(31556926);
//        registry.addResourceHandler("/front/js/**").addResourceLocations("classpath:/static/front/js/").setCachePeriod(31556926);
//        registry.addResourceHandler("/upload/**").addResourceLocations("/upload/").setCachePeriod(31556926);
//        registry.addResourceHandler("/html/**").addResourceLocations("classpath:/static/html/").setCachePeriod(31556926);
//        registry.addResourceHandler("/html2/**").addResourceLocations("/html2/").setCachePeriod(31556926);
//        super.addResourceHandlers(registry);
//        String os = System.getProperty("os.name");
//
//        if (os.toLowerCase().startsWith("win")) {  //如果是Windows系统
//            registry.addResourceHandler("/smallapple/**")
//                    // /apple/**表示在磁盘apple目录下的所有资源会被解析为以下的路径
//                    .addResourceLocations("file:G:/itemsource/smallapple/") //媒体资源
//                    .addResourceLocations("classpath:/META-INF/resources/");  //swagger2页面
//        } else {  //linux 和mac
//            registry.addResourceHandler("/smallapple/**")
//                    .addResourceLocations("file:/resources/smallapple/")   //媒体资源
//                    .addResourceLocations("classpath:/META-INF/resources/");  //swagger2页面;
//        }
//    }

